<?

include 'server_config.php'; 

$board_name=$_GET['name'];
$category_idx=$_GET['category_idx'];
$page=$_GET['page'];

if($board_name=='')
	return;

if($category_idx=='')
	$category_idx=0;

if($page=='')
	$page=1;

$sql="
	SELECT board_idx, title, content FROM ndd_board WHERE name = '$board_name';
";

$result = mysql_query($sql,$connect);

$row = mysql_fetch_row($result);

$board_idx = $row[0];
$board_title = $row[1];
$board_content = $row[2];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="Stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./js/jquery-1.6.2.min.js"></script>
	<script>
	function talk_display_article(document_idx)
	{
//		alert(documnet_idx);

		$('#content').load('ndd_talk_document.php', { 'document_idx' : document_idx});

	}
	</script>
	<script type="text/javascript">
		dayName = ["일요일", "월요일", "화요일", "수요일", "목요일", "금요일", "토요일"];
		second = 1000;
		minute = second * 60;
		hour = minute * 60;
		day = hour * 24;

		getTimestring = function(time) {

		var now = new Date();
		var diff = now - time;

		var dhour = Math.floor(diff / hour);
		diff -= dhour * hour;
		var dminute = Math.floor(diff / minute);
		diff -= dminute * minute;
		var dsecond = Math.floor(diff / second);
		diff -= dsecond * second;

		var timeStringKorean = "";
		  timeStringKorean = time.getFullYear() + "년 ";
		  timeStringKorean += (time.getMonth() + 1) + "월 ";
		  timeStringKorean += time.getDate() + "일 " + dayName[time.getDay()] + " ";
		  var timeTimeStringKorean = "";
		  if (time.getHours() < 12)
		  {
			timeTimeStringKorean += "오전 ";
			if (time.getHours() == 0)
			  timeTimeStringKorean += "12:";
			else if (time.getHours() < 10)
			  timeTimeStringKorean += "0" + time.getHours() + ":";
			else
			  timeTimeStringKorean += time.getHours() + ":"
		  }
		  else
		  {
			timeTimeStringKorean += "오후 ";
			if (time.getHours() == 12)
			  timeTimeStringKorean += "12:";
			else if (time.getHours() < 22)
			  timeTimeStringKorean += "0" + (time.getHours() - 12) + ":";
			else
			  timeTimeStringKorean += (time.getHours() - 12) + ":";
		  }
		  if (time.getMinutes() >= 10)
			timeTimeStringKorean += time.getMinutes();
		  else
			timeTimeStringKorean += "0" + time.getMinutes();
		  timeTimeStringKorean += ":"
		  if (time.getSeconds() >= 10)
			timeTimeStringKorean += time.getSeconds();
		  else
			timeTimeStringKorean += "0" + time.getSeconds();
		timeStringKorean += timeTimeStringKorean;

		var timestring;
		if (time.toDateString() != now.toDateString())
		{
		  now.setHours(0);
		  now.setMinutes(0);
		  now.setSeconds(0);
		  diff = now - time;
		  var dday = Math.ceil(diff / day);
		  if (dday == 1)
			timestring = "어제 " + timeTimeStringKorean.replace(/:..$/, '');
		  else if (now.getDay() - dday >= 1 && dday <= 3)
		  {
			timestring = dayName[time.getDay()] + " " + timeTimeStringKorean.replace(/:..$/, '');
		  }
		  else
		  {
			if (time.getYear() < now.getYear())
			  timestring = timeStringKorean.replace(/일 .+일/, '일').replace(/:..$/, ''); 
			else
			  timestring = timeStringKorean.replace(/.+년 /, '').replace(/일 .+일/, '일').replace(/:..$/, '');
		  }
		}
		else if (dhour > 1)
		  timestring = dhour + "시간 전";
		else if (dhour == 1)
		  timestring = "약 1시간 전";
		else if (dminute > 1)
		  timestring = dminute + "분 전";
		else if (dminute == 1)
		  timestring = "약 1분 전";
		else if (dsecond > 0)
		  timestring = dsecond + "초 전";

		return timestring;

		};
		function getDateStr(y,m,d,h,min,s) {
		  var res = new Date(y, m-1, d, h, min, s);
//		  alert(m);
		  //alert(h + ' ' + min + ' ' + s);
//		  var d = new Date(date_string);

//		alert(res);
			return getTimestring(res);
		}
		var d = new Date(2011, 07, 22, 00, 11);

	</script>
</head>
<body>
    <div id="wrapper">
        <div id="top-menu-wrapper">
            <div id="top-menu">
                <div id="logo">next door doctor</div>
                <a href="#"><div id="top-menu-1"></div></a>
                <a href="#"><div id="top-menu-2"></div></a>
                <a href="#"><div id="top-menu-3"></div></a>
		<div id="search-box-wrapper">
                    <input type="text" id="search-box" />
		</div>
                <a href="#"><div id="top-menu-4"></div></a>
                <a href="#"><div id="top-menu-5"></div></a>
                <div id="top-right-menu-wrapper">
                    <a href="#"><div id="top-right-menu-1"><div class="right-count">41</div></div></a>
                    <a href="#"><div id="top-right-menu-2"><div class="right-count">2</div></div></a>
                    <a href="#"><div id="top-right-menu-3"><div class="right-count">3</div></div></a>
                </div>
            </div>
        </div>


<?

				$sql_count ="
					SELECT COUNT(A.document_idx)
					FROM ndd_document A, ndd_user B
					WHERE A.board_idx = '$board_idx' 
					AND A.user_idx = B.user_idx
					";

				if($category_idx!=0)
					$sql_count .="	AND A.category_idx= ".$category_idx." ";

				if($search_type==1)
					$sql_count .="	AND A.title like '%".$search_input."%' ";
				if($search_type==2)
					$sql_count .="	AND A.content like '%".$search_input."%' ";
				if($search_type==3)
					$sql_count .="	AND ( A.title like '%".$search_input."%' OR A.content like '%".$search_input."%' ) ";
				if($search_type==4)
					$sql_count .="	AND B.nickname like '%".$search_input."%' ";


				$sql_count.="
					;
				";

				$result = mysql_query($sql_count,$connect);
				$row = mysql_fetch_row($result);

				$article_count = $row[0];
?>

		<div id="list">
		
			<div style='height:2px;width:100%;background:#cccccc;float:left;'><span><span></div>
			<div style='height:3px;width:100%;float:left;'><span><span></div>
			<div style='height:1px;width:100%;background:#cccccc;float:left;'><span><span></div>

<?

			

				$sql="
					SELECT A.document_idx, A.title, A.category_idx, A.read_count, B.nickname, A.recommend_count, A.accuse_count, A.reg_timestamp, C.name, A.comment_count, A.user_idx, A.content
					FROM ndd_document A, ndd_user B, ndd_category C					
					WHERE A.board_idx = '$board_idx' 
					AND A.user_idx = B.user_idx
					AND A.category_idx = C.category_idx 
					";

				if($category_idx!=0)
					$sql .="	AND A.category_idx= ".$category_idx." ";

				if($search_type==1)
					$sql .="	AND A.title like '%".$search_input."%' ";
				if($search_type==2)
					$sql .="	AND A.content like '%".$search_input."%' ";
				if($search_type==3)
					$sql .="	AND ( A.title like '%".$search_input."%' OR A.content like '%".$search_input."%' ) ";
				if($search_type==4)
					$sql .="	AND B.nickname like '%".$search_input."%' ";

				$sql.="
					ORDER BY A.reg_timestamp DESC LIMIT ".($page-1)*$article_per_page.", ".$article_per_page.";
				";

				$result = mysql_query($sql,$connect);

				$i=0;

				while($row = mysql_fetch_row($result))
				{
					if($i==0)
					{
						$document_idx = $row[0];
						?>
						<script>
							
							//setTimeout('talk_display_article(<? echo $row[0];?>);',10);
							$(document).ready(function(){ talk_display_article(<? echo $row[0];?>); });
						</script>
						<?
					}
					$i++;
			
?>


			<div style='height:99px;width:100%;float:left;cursor:pointer;' onmouseover="style.background='#F2F2F2';$('#list_arrow_<? echo $row[0];?>').show();" onmouseout="style.background='#FFFFFF';$('#list_arrow_<? echo $row[0];?>').hide();" onclick='talk_display_article(<? echo $row[0];?>);'>
				<div style='height:15px;width:100%;float:left;'><span>
					<!--div style='height:35px;width:35px;position:absolute;background:red;'><span></span></div-->
				</span></div>
				<div style='height:84px;width:65px;float:left;'>
					<div style='height:84px;width:14px;float:left;'></div>
					<div style='height:38px;width:38px;float:left;background:yellow;'>
					
					
						<?
							$document_user_idx=$row[10];
							$user_img = "../ndd/upload/member_photo/".$document_user_idx;

							if(file_exists($user_img)){
						?>
							<img src="<? echo $user_img; ?>" width="38px" height="38px" border=0>
						<?
							}
							else
							{
						?>
							<? if($document_user_idx == $_SESSION['user_idx'])
								{
							?>
								<a href="member_info.php">이미지등록</a>
							<?
								}
							?>
						<?
							}
						?>					
					
					</div>
				</div>
				<div style='height:84px;width:140px;float:left;'>
					<span id='list_id'><? echo $row[4]; //글쓴이?></span>
					<span id='list_id_detail'><br>
						<? 
						
							$sql_2="
								SELECT birthday, sex FROM ndd_user_expand WHERE user_idx = $document_user_idx LIMIT 1;
							";
									
							$result_2 = mysql_query($sql_2,$connect);
				
							$row2 = mysql_fetch_row($result_2);

							if($row2[1]=="M")
								echo "남";
							else if($row[1]=="F")
								echo "여";
							else 
								echo "(미입력)";
							
							echo " / ";

							if($row[0]!="0000-00-00" && $row2[0]!="")
							{

								//생일 입력
								$myBirthDate    = strtotime($row2[0]);

								//만나이
								$birthDate1     = date( 'Ymd', $myBirthDate );
								$nowDate1       = date('Ymd');
								$age1           = floor(($nowDate1 - $birthDate1) / 10000);

								//한국 나이
								$birthDate2     = date( 'Y', $myBirthDate );
								$nowDate2       = date('Y');
								$age2           = $nowDate2 - $birthDate2 + 1 ;

								echo $age2."세";
							}
							else
							{
								echo "(미입력)";

							}
				
						?>

						<br>
						류마 점수 3점<br>

					</span>
				</div>
				<div style='height:54px;width:310px;float:left;'>
					<span id='list_article_title'><? echo $row[1]; //제목?> <? //echo $row[5]; //추천수?><br></span>
					<span id='list_article_content'>
					<? echo iconv_substr($row[11],0,60,"UTF-8");
// substr($row[11],0,120); //내용?>...<br>
					</span>


				</div>
				<div style='height:54px;width:50px;float:left;display:none;' id='list_arrow_<? echo $row[0];?>'>
					<img src='./img/list_arrow.jpg'>
					
				</div>
				<div style='height:30px;width:310px;float:left;'>
					<span id='list_article_title'>
						<? //echo $row[7]; //등록시각?>
						<script>
							document.write( getDateStr(<? echo substr($row[7],0,4); ?>, <? echo substr($row[7],5,2); ?>, <? echo substr($row[7],8,2); ?>, <? echo substr($row[7],11,2); ?>, <? echo substr($row[7],14,2); ?>, <? echo substr($row[7],17,2); ?>) );
						</script>
					</span>
				</div>
			

			</div>
			<div style="height:1px;width:100%;background-image:url('./img/line_dot.png');float:left;"><span><span></div>
<?
				}
?>


		리스트<br />
		</div>
		<div id="fixed-container">
			<div class="content-viewer">
				<div class="toolbar">
				
				</div>
				<div id="content">
				</div>
			</div>
		</div>
    </div>

	
	<iframe src='' name='ifr_temp' style='display:none;'></iframe>



</body>
<script>
$("#content").css("height", $(window).height()-150);
$(window).resize(function() {
$("#content").css("height", $(window).height()-150);
});
</script>
</html>
